Imports System.Configuration
Imports System.IO
Imports System.Data.SqlClient
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Collections

Public Class DBAccess
    Private m_strConnection As String

    'key to the application is from the webconfig
    Friend Sub New()
        m_strConnection = DSNManager.GetConnectionString()
    End Sub 'New

    'This method has run level transaction management. This means that if you have a group of
    'dependant SQL statements which all must be executed to create an 'entity' then they must 
    'be batched in to the command string being called.
    Friend Sub RunSQL(ByVal strSQL As String, ByVal cmdType As CommandType)
        ' Log.Out(Log.SQLDEBUG, Me, "RUNSQL: " & strSQL)
        Dim cmd As IDbCommand
        Dim conn As IDbConnection

        Try
            cmd = TestProviderFactory.GetCommand(strSQL, cmdType)
            conn = TestProviderFactory.GetConnection(m_strConnection)
            cmd.Connection = conn
            Try
                cmd.ExecuteNonQuery()
            Catch ex As SqlException
                Throw New DOException("RunSQL:Failed SQL write.", strSQL, ex)
            End Try
        Catch ex As SqlException
            Throw New DOException("RunSQL:Failed Connection.", strSQL, ex)
        Finally
            If Not conn Is Nothing Then
                conn.Close()
            End If
            If Not cmd Is Nothing Then
                cmd.Dispose()
            End If
        End Try
    End Sub


    Friend Function RunSQLReturnDataSet(ByVal strSQL As String, ByVal cmdtype As CommandType) As DataSet
        'Log.Out(Log.SQLDEBUG, Me, "READ: " & strSQL)
        Try
            Dim ds As New DataSet("DataSet")
            Dim da As IDbDataAdapter
            da = TestProviderFactory.GetAdapter(strSQL, m_strConnection)
            da.Fill(ds)
            Return ds
        Catch ex As SqlException
            Throw New DOException("Run SQL - ReturnDataSet.", strSQL, ex)
        End Try

    End Function
End Class

